package com.minazuki.signinserver.web;

import com.minazuki.signinserver.config.MqttProperties;
import com.minazuki.signinserver.mqtt.Mqtt;
import com.minazuki.signinserver.pojo.dao.PublishRecord;
import com.minazuki.signinserver.pojo.msg.Msg;
import com.minazuki.signinserver.pojo.msg.SignMessage;
import com.minazuki.signinserver.service.CourseService;
import com.minazuki.signinserver.service.PublishService;
import com.minazuki.signinserver.utils.JsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("publish")
public class PublishController {
    private static final Logger logger = LoggerFactory.getLogger(CourseController.class);

    @Autowired
    PublishService ps;
    @Autowired
    CourseService courseService;
    @Autowired
    MqttProperties config;
    @Autowired
    private Mqtt.MqttGateway mqtt;

    @GetMapping("list/{cid}")
    public Msg getPublishRecord(@PathVariable int cid, @CookieValue(name = "SIGNSYS_TOKEN") String token) {
        Msg msg = new Msg();
        List<PublishRecord> records = ps.getPublish(cid, token);
        if (!records.isEmpty()) {
            msg.setPublishRecords(records);
        } else msg.setMsg("签到发起记录为空");
        return msg;
    }

    @GetMapping("signlist/{cid}")
    public Msg getPublishRecordForSub(@PathVariable int cid, @CookieValue(name = "SIGNSYS_TOKEN") String token) {
        Msg msg = new Msg();
        List<PublishRecord> records = ps.getPublishForSub(cid, token);
        if (!records.isEmpty()) {
            msg.setPublishRecords(records);
        } else msg.setMsg("签到发起记录为空");
        return msg;
    }

    @PostMapping("add")
    public Msg addPublishRecord(@Valid @RequestBody PublishRecord record, @CookieValue(name = "SIGNSYS_TOKEN") String token) {
        Msg msg = new Msg();
        SignMessage signMessage = new SignMessage();
        if (ps.addPublish(record, token)) {
            signMessage.setCid(record.getCid());
            signMessage.setTitle("新签到");
//            signMessage.setCode(0);
            signMessage.setCourseName(courseService.queryCourseName(record.getCid()));
            signMessage.setMsg(signMessage.getCourseName() + "有新的签到");
            mqtt.sendToMqtt("cid/" + record.getCid(), JsonUtils.serialize(signMessage));
        } else {
            msg.setMsg("添加失败");
        }
        return msg;
    }

    @GetMapping("end/{pid}")
    public ResponseEntity<Msg> endSign(@PathVariable int pid, @CookieValue(name = "SIGNSYS_TOKEN") String token) {
        Msg msg = new Msg();
        if (ps.endPublish(pid, token)) {
            msg.setMsg("结束签到成功");
        } else msg.setMsg("结束签到失败");
        return ResponseEntity.ok(msg);

    }

    @GetMapping("delay/{pid}/{time}")
    public ResponseEntity<Msg> delaySign(@PathVariable int pid, @PathVariable int time, @CookieValue(name = "SIGNSYS_TOKEN") String token) {
        logger.info("签到号" + pid + "延时" + time + "分钟");
        Msg msg = new Msg();
        if (ps.delayPublish(pid, time, token)) {
            msg.setMsg("延长" + time + "分钟成功");
        } else {
            msg.setMsg("延长失败");
        }
        return ResponseEntity.ok(msg);

    }

    @GetMapping("mqtt/test/{mmsg}")
    public ResponseEntity<Msg> mqttTest(@PathVariable String mmsg) {
        Msg msg = new Msg();
        msg.setMsg("Mqtt Test:" + mmsg);
        mqtt.sendToMqtt("signsys", "test:" + mmsg);
        return ResponseEntity.ok(msg);
    }


}
